/**
 * Created by qunxing.du on 2016/7/18.
 * Copyright 2016 汉威.智慧环保事业部 Limited.
 * All rights reserved.
 */
package com.hw.one.collect.gdyc.data;

import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 广德扬尘原始5min钟数据
 */
@Service
public class GdRawFiveMinDataService extends GdDataService{

    public String pull(Map<String,Object> map){
        List<Map> list = find(map);
        return parseData(list);
    }

    private List<Map> find(Map<String,Object> map){
        //获取自己设备码
        Object deviceCode = getDevices(DEVICES).keySet().toArray()[0];
        Object startTime = map.get("startTime");
        Object endTime = map.get("endTime");
        Query query =  Query.query(Criteria.where("SNCode").is(deviceCode).and("receiveTime").gte(startTime).lte(endTime))
                .with(new Sort(Sort.Direction.DESC,"receiveTime"));
        String collectionName = getCollection();
        List<Map> list = dqjcMongoTemplate.find(query, Map.class,collectionName);
        log.debug("查询数据数量："+list.size());
        return list;
    }

    /**
     * 解析数据
     * @param list
     * @return
     */
    private String parseData(List<Map> list){
        if(list == null || list.size() == 0 ){
            return null;
        }
        Map<String,String> factorsMap = getFactors(FACTORS);
        StringBuffer factorSb = new StringBuffer();
        String deviceCode = null;
        String receiveTime = null;
        for (Map map : list) {
            if(deviceCode == null){
                deviceCode = (String) map.get("SNCode");
            }
            if(receiveTime == null){
                receiveTime = (String) map.get("receiveTime");
            }
            List<Map> devList = (ArrayList<Map>) map.get("devList");
            for (Map device : devList) {
                String mediaType = (String) device.get("mediaType");
                String chanlValue = (String) device.get("chanlValue");
                if(factorsMap.containsKey(mediaType)){
                    factorSb.append(FACTOR_SEPARATOR).append(factorsMap.get(mediaType)).append(VAL_SEPARATOR).append(chanlValue);
                    factorsMap.remove(mediaType);
                }
                log.debug("因子值："+mediaType+"|"+chanlValue);
                break;
            }
        }
        Iterator<String> thirdFactos = factorsMap.values().iterator();
        while (thirdFactos.hasNext()){
            String thirdFactor = thirdFactos.next();
            factorSb.append(FACTOR_SEPARATOR).append(thirdFactor).append(VAL_SEPARATOR).append("0");
        }
        StringBuffer sb = new StringBuffer();
        sb.append("DevID").append(VAL_SEPARATOR).append(getDevices(DEVICES).get(deviceCode)).append(FACTOR_SEPARATOR).append("Time").append(VAL_SEPARATOR).append(receiveTime)
        .append(factorSb);
        return sb.toString();
    }



}
